<template>
  <div class="star-rating" :class="sizeClass">
    <span
      v-for="(star, index) in stars"
      :key="index"
      class="star"
      :class="{
        'star-filled': star.filled,
        'star-half': star.half,
        'star-disabled': disabled
      }"
      @click="!disabled && handleClick(index + 1)"
      @mouseover="!disabled && handleHover(index + 1)"
      @mouseleave="!disabled && handleMouseLeave()"
    >
      <StarFilled v-if="star.filled" />
      <StarOutlined v-else-if="star.half" class="star-half-icon" />
      <StarOutlined v-else />
    </span>
    
    <span v-if="showText" class="star-text">
      {{ ratingText }}
    </span>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { StarFilled, StarOutlined } from '@ant-design/icons-vue'

interface Props {
  value?: number
  max?: number
  allowHalf?: boolean
  disabled?: boolean
  size?: 'small' | 'default' | 'large'
  showText?: boolean
  texts?: string[]
}

interface Emits {
  (e: 'update:value', value: number): void
  (e: 'change', value: number): void
}

const props = withDefaults(defineProps<Props>(), {
  value: 0,
  max: 5,
  allowHalf: false,
  disabled: false,
  size: 'default',
  showText: false,
  texts: () => ['极差', '失望', '一般', '满意', '惊喜']
})

const emit = defineEmits<Emits>()

const hoverValue = ref(0)
const isHovering = ref(false)

const currentValue = computed(() => {
  return isHovering.value ? hoverValue.value : props.value
})

const stars = computed(() => {
  const stars = []
  for (let i = 1; i <= props.max; i++) {
    const filled = i <= Math.floor(currentValue.value)
    const half = props.allowHalf && i === Math.ceil(currentValue.value) && currentValue.value % 1 !== 0
    
    stars.push({
      filled: filled && !half,
      half
    })
  }
  return stars
})

const sizeClass = computed(() => {
  return `star-rating-${props.size}`
})

const ratingText = computed(() => {
  if (props.texts && props.texts.length >= props.max) {
    const index = Math.ceil(currentValue.value) - 1
    return props.texts[index] || ''
  }
  return `${currentValue.value}/${props.max}`
})

const handleClick = (value: number) => {
  if (props.allowHalf) {
    // 允许半星评分的逻辑
    const isSecondClick = value === props.value
    const newValue = isSecondClick ? value - 0.5 : value
    emit('update:value', Math.max(0, newValue))
    emit('change', Math.max(0, newValue))
  } else {
    emit('update:value', value)
    emit('change', value)
  }
}

const handleHover = (value: number) => {
  hoverValue.value = value
  isHovering.value = true
}

const handleMouseLeave = () => {
  isHovering.value = false
  hoverValue.value = 0
}
</script>

<style scoped>
.star-rating {
  display: inline-flex;
  align-items: center;
  gap: 2px;
}

.star {
  cursor: pointer;
  color: #d9d9d9;
  transition: color 0.2s;
  position: relative;
}

.star:hover {
  color: #fadb14;
}

.star-filled {
  color: #fadb14;
}

.star-half {
  color: #fadb14;
}

.star-half-icon {
  position: relative;
}

.star-half-icon::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 50%;
  height: 100%;
  background: #fadb14;
  z-index: 1;
}

.star-disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

.star-disabled:hover {
  color: inherit;
}

.star-text {
  margin-left: 8px;
  color: #666;
  font-size: 14px;
}

/* 尺寸样式 */
.star-rating-small .star {
  font-size: 14px;
}

.star-rating-default .star {
  font-size: 18px;
}

.star-rating-large .star {
  font-size: 24px;
}

.star-rating-small .star-text {
  font-size: 12px;
}

.star-rating-large .star-text {
  font-size: 16px;
}
</style> 